package org.spring.springboot.service.impl;

import org.spring.springboot.dao.CategoryDao;
import org.spring.springboot.dao.CityDao;
import org.spring.springboot.domain.Category;
import org.spring.springboot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.soap.Node;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 栏目分类 DAO 接口类
 *
 * Created by 赵新国 on 07/02/2017.
 */
@Service
public class CategoryServiceImpl implements CategoryService{

    private List<Category> returnList = new ArrayList<Category>();

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<Category> findAllCategory() {
        return categoryDao.findAllCategory();
    }

    @Override
    public Long saveCategory(Category category) {
        return categoryDao.saveCategory(category);
    }

    @Override
    public Category findCategory(Integer id) {
        return categoryDao.findCategory(id);
    }

    @Override
    public Category getTreeNodes(Integer id) {
        // 根据id获取节点对象(SELECT * FROM category c WHERE c.id=id)
        Category category = categoryDao.findCategoryById(id);
        // 查询id下的所有子节点(SELECT * FROM category c WHERE c.parent_id=id)
        List<Category> childTreeList = categoryDao.getChildNodes(id);
        // 遍历子节点
        for(Category child : childTreeList){
            Category treeNodes = getTreeNodes(child.getId());
            category.getChildCategoryList().add(treeNodes);
        }
        return category;
    }

    @Override
    public Category getCategoryById(Integer id) {
        return categoryDao.findCategory(id);
    }

}
